####################################################################################################################
####Ash & Turcu Figure Replicaton###################################################################################
####Who Votes for Populist Presidential Candidates? Differential Support among US-based Latin American Diasporas####
####Forthcoming in Political Geography##############################################################################



###Figure A1: How political socialization affects origin country candidate-level diaspora voting in the#############
###US compared to origin country voting for left-economic candidates################################################

conf=.95
title="Effect of Political Socialization conditional on Education"
ylabel="Diaspora Vote Share of Leftist Candidate"
xlabel="Average Level of Education"

# Get coefficients of variables
beta_1 =   3.222722 
beta_3 =   -.19741

# Set range of the moderator variable
# Minimum

min_val = 12
max_val = 21



# Create list of moderator values at which marginal effect is evaluated
x_2 <- seq(from=min_val, to=max_val, by=1)

# Compute marginal effects
delta_1 = beta_1 + beta_3*x_2

# Compute variances
var_1 =        1.5260921   + (x_2^2)*  .00483765 + 2*x_2*   -.08478376     

# Standard errors
se_1 = sqrt(var_1)

# Upper and lower confidence bounds
z_score = qnorm(1 - ((1 - conf)/2))
upper_bound = delta_1 + z_score*se_1
lower_bound = delta_1 - z_score*se_1

# Determine the bounds of the graphing area
max_y = max(upper_bound)
min_y = min(lower_bound)

# Initialize plotting window

plot(x=c(), y=c(), ylim=c(min_y, max_y), xlim=c(min_val, max_val), xlab=xlabel, ylab=ylabel, main=title, xaxt='n')

axis(side = 1, 
     at = 14,# Draw x-axis
     c( "Some High School"))

axis(side = 1, 
     at = 16,# Draw x-axis
     c( "High School"))

axis(side = 1, 
     at = 18,# Draw x-axis
     c( "Some College"))

axis(side = 1, 
     at = 21,# Draw x-axis
     c( "College Degree"))

# Plot estimated effects
lines(y=delta_1, x=x_2)
lines(y=upper_bound, x=x_2, lty=2)
lines(y=lower_bound, x=x_2, lty=2)

# Add a dashed horizontal line for zero
abline(h=0, lty=3)

###Figure A2: How political socialization affects origin country candidate-level diaspora voting in the#############
###US compared to origin country voting for populist candidates################################################



conf=.95
title="Effect of Socialization based on Destination Country Populism"
xlabel="Origin Country Election Year"
ylabel="Likelihood of Diaspora Support for Populist Candidate"
factor_labels=c("Before 2015","2015 and after")


# Get coefficients of variables
beta_1 =     .3074249 
beta_3 =    -.6313498 

# Create list of moderator values at which marginal effect is evaluated
x_2 <- c(0,1)

# Compute marginal effects
delta_1 = beta_1 + beta_3*x_2

# Compute variances
var_1 =    .24287316   + (x_2^2)*   -.10511971   + 2*x_2* .09815864    

# Standard errors
se_1 = se_1 = sqrt(var_1)

# Upper and lower confidence bounds
z_score = qnorm(1 - ((1 - conf)/2))
upper_bound = delta_1 + z_score*se_1
lower_bound = delta_1 - z_score*se_1

# Determine the bounds of the graphing area
max_y = max(upper_bound)
min_y = min(lower_bound)

# Initialize plotting window
plot(x=c(), y=c(), ylim=c(min_y, max_y), xlim=c(-.5, 1.5), xlab=xlabel, ylab=ylabel, main=title, xaxt="n")

# Plot points of estimated effects
points(x=x_2, y=delta_1, pch=16)

# Plot lines of confidence intervals
lines(x=c(x_2[1], x_2[1]), y=c(upper_bound[1], lower_bound[1]), lty=1)
points(x=c(x_2[1], x_2[1]), y=c(upper_bound[1], lower_bound[1]), pch=c(25,24), bg="black")
lines(x=c(x_2[2], x_2[2]), y=c(upper_bound[2], lower_bound[2]), lty=1)
points(x=c(x_2[2], x_2[2]), y=c(upper_bound[2], lower_bound[2]), pch=c(25,24), bg="black")

# Label the axis
axis(side=1, at=c(0,1), labels=factor_labels)

# Add a dashed horizontal line for zero
abline(h=0, lty=3)

###Figure A3: How political socialization affects origin country candidate-level diaspora voting in the#############
###US compared to origin country voting for left-economic candidates################################################



conf=.95
title="Effect of Political Socialization conditional on Age at Arrival"
ylabel="Diaspora Vote Share of Leftist Candidate"
xlabel="Average Age at Arrival"

# Get coefficients of variables
beta_1 =    7.372824 
beta_3 =    -.2977573 

# Set range of the moderator variable
# Minimum

min_val = 20
max_val = 28



# Create list of moderator values at which marginal effect is evaluated
x_2 <- seq(from=min_val, to=max_val, by=1)

# Compute marginal effects
delta_1 = beta_1 + beta_3*x_2

# Compute variances
var_1 =         6.4441026   + (x_2^2)*   .00904381 + 2*x_2*   -.24090071     

# Standard errors
se_1 = sqrt(var_1)

# Upper and lower confidence bounds
z_score = qnorm(1 - ((1 - conf)/2))
upper_bound = delta_1 + z_score*se_1
lower_bound = delta_1 - z_score*se_1

# Determine the bounds of the graphing area
max_y = max(upper_bound)
min_y = min(lower_bound)

# Initialize plotting window

plot(x=c(), y=c(), ylim=c(min_y, max_y), xlim=c(min_val, max_val), xlab=xlabel, ylab=ylabel, main=title, xaxt='n')

axis(side = 1, 
     at = 20,# Draw x-axis
     c( "20"))

axis(side = 1, 
     at = 22,# Draw x-axis
     c( "22"))

axis(side = 1, 
     at = 24,# Draw x-axis
     c( "24"))

axis(side = 1, 
     at = 26,# Draw x-axis
     c( "26"))

axis(side = 1, 
     at = 28,# Draw x-axis
     c( "28"))

# Plot estimated effects
lines(y=delta_1, x=x_2)
lines(y=upper_bound, x=x_2, lty=2)
lines(y=lower_bound, x=x_2, lty=2)

# Add a dashed horizontal line for zero
abline(h=0, lty=3)


###Figure A4: How political socialization affects origin country candidate-level diaspora voting in the#############
###US compared to origin country voting for right-economic candidates################################################




conf=.95
title="Effect of Political Socialization conditional on Age at Arrival"
ylabel="Diaspora Vote Share of Rightist Candidate"
xlabel="Average Age at Arrival"

# Get coefficients of variables
beta_1 =      1.688402
beta_3 =     -.0554947  

# Set range of the moderator variable
# Minimum

min_val = 20
max_val = 28



# Create list of moderator values at which marginal effect is evaluated
x_2 <- seq(from=min_val, to=max_val, by=1)

# Compute marginal effects
delta_1 = beta_1 + beta_3*x_2

# Compute variances
var_1 =         .56683695   + (x_2^2)*    .00062872   + 2*x_2*   -.0186671     

# Standard errors
se_1 = sqrt(var_1)

# Upper and lower confidence bounds
z_score = qnorm(1 - ((1 - conf)/2))
upper_bound = delta_1 + z_score*se_1
lower_bound = delta_1 - z_score*se_1

# Determine the bounds of the graphing area
max_y = max(upper_bound)
min_y = min(lower_bound)

# Initialize plotting window

plot(x=c(), y=c(), ylim=c(min_y, max_y), xlim=c(min_val, max_val), xlab=xlabel, ylab=ylabel, main=title, xaxt='n')

axis(side = 1, 
     at = 20,# Draw x-axis
     c( "20"))

axis(side = 1, 
     at = 22,# Draw x-axis
     c( "22"))

axis(side = 1, 
     at = 24,# Draw x-axis
     c( "24"))

axis(side = 1, 
     at = 26,# Draw x-axis
     c( "26"))

axis(side = 1, 
     at = 28,# Draw x-axis
     c( "28"))

# Plot estimated effects
lines(y=delta_1, x=x_2)
lines(y=upper_bound, x=x_2, lty=2)
lines(y=lower_bound, x=x_2, lty=2)

# Add a dashed horizontal line for zero
abline(h=0, lty=3)


